//
//  FKCommodityServiceView.m
//  BaiYeMallShop
//
//  Created by Macbook Pro on 2018/11/6.
//  Copyright © 2018年 Macbook Pro. All rights reserved.
//

#import "FKCommodityServiceView.h"
#import "FKCommodityServiceCell.h"

@interface FKCommodityServiceView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation FKCommodityServiceView

- (NSArray *)datas
{
    if (!_datas) {
        _datas = @[@{@"name" : @"金牌服务", @"value" : @"24小时贴心管家服务，让您买的放心"},
                   @{@"name" : @"闪电配送", @"value" : @"急速配送服务"},
                   @{@"name" : @"正品保障", @"value" : @"该商品由“有一说易”承保正品保障"}];
    }
    return _datas;
}

+ (instancetype)serviceView
{
    FKCommodityServiceView *view = [[self alloc] init];
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(confirmBtnClicked)]];
    
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, fkScreenH, fkScreenW, fkScreenH * 0.5)];
    [self addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:fkColor333333 textAlignment:NSTextAlignmentCenter text:@"基础保障"];
    [container addSubview:titleLabel];
    titleLabel.frame = CGRectMake(0, 0, fkScreenW, fk_adjustW(44));
    
    UIView *separator = [UIView fk_lineWithFrame:CGRectMake(0, titleLabel.fk_bottom - 0.8, fkScreenW, 0.8)];
    [container addSubview:separator];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    [container addSubview:tableView];
    self.tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [UIView new];
    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    UIButton *confirmBtn = [UIButton fk_btnWithTarget:self action:@selector(confirmBtnClicked) font:fk_adjustFont(16) normalTitle:@"确定" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage fk_imageWithColor:fkMainColor] selectedBgImg:nil];
    [container addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(container);
        make.height.mas_equalTo(fk_adjustW(44));
    }];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(titleLabel.mas_bottom);
        make.bottom.equalTo(confirmBtn.mas_top);
    }];
}

- (void)confirmBtnClicked
{
    [UIView animateWithDuration:0.25 animations:^{
        self.container.fk_top = fkScreenH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)show
{
    [self.tableView reloadData];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    [UIView animateWithDuration:0.25 animations:^{
        self.container.frame = CGRectMake(0, fkScreenH * 0.5, fkScreenW, fkScreenH * 0.5);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKCommodityServiceCell *cell = [FKCommodityServiceCell cellWith:tableView];
    cell.data = self.datas[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(60);
}


@end
